//
//  LabelView.m
//  AlarmClock
//
//  Created by apple on 16/10/8.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "LabelView.h"
#import "SDAutoLayout.h"
#import "UIViewController+Tools.h"
@interface LabelView  ()
@property (nonatomic,strong) UIView *backView;
@property (nonatomic,strong) UILabel *titleLabel;
@property (nonatomic,strong) UITextField *textField;
@property (nonatomic,strong) UIButton *cancelButton;
@property (nonatomic,strong) UIButton *sureButton;
@end
@implementation LabelView

- (void)show:(CGRect)rect {
    [self addSubview:self.backView];
    self.backgroundColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.51];
    self.backView.frame = rect;
    self.backView.backgroundColor = [UIColor whiteColor];
    [self.backView addSubview:self.titleLabel];
    [self.backView addSubview:self.textField];
    [self.backView addSubview:self.cancelButton];
    [self.backView addSubview:self.sureButton];
    
    
    self.titleLabel.sd_layout
    .topSpaceToView(self.backView,10)
    .leftSpaceToView(self.backView,0)
    .widthIs(rect.size.width)
    .heightIs(30);
    
    self.textField.sd_layout
    .topSpaceToView(self.titleLabel,10)
    .leftSpaceToView(self.backView,20)
    .widthIs(rect.size.width - 40)
    .heightIs(rect.size.width / 6);
    
    self.cancelButton.sd_layout
    .leftSpaceToView(self.backView,20)
    .widthIs((rect.size.width - 50)/2)
    .bottomSpaceToView(self.backView,10)
    .heightIs(rect.size.width / 6);
    
    self.sureButton.sd_layout
    .bottomSpaceToView(self.backView,10)
    .leftSpaceToView(self.cancelButton,10)
    .widthIs((rect.size.width - 50)/2)
    .heightIs(rect.size.width / 6);
    
    [UIView animateWithDuration:0.4 animations:^{
        self.transform = CGAffineTransformMakeScale(1, 1);
        self.backView.alpha = 1;
    }];
    
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    [self addGestureRecognizer:tap];
    UITapGestureRecognizer * emptyTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(emtyAction:)];
    [self.backView addGestureRecognizer:emptyTap];
}

-(void)emtyAction:(UITapGestureRecognizer*)gesture{
    
}

-(void)tapAction:(UITapGestureRecognizer*)gesture{
    [UIView animateWithDuration:0.3 animations:^{
        self.backView.transform = CGAffineTransformMakeScale(0.1, 0.1);
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark - action
- (void)cancelButtonPressed:(UIButton *)sender {
    [UIView animateWithDuration:0.3 animations:^{
        self.backView.transform = CGAffineTransformMakeScale(0.1, 0.1);
        self.backView.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)sureButtonPressed:(UIButton *)sender {
    if ([self.textField.text isEqualToString:@""]) {
        [self.controller getAlertController:@"温馨提示" error:@"请输入标签内容"];
    } else {
        if(self.labelViewCallback){
            self.labelViewCallback(self.textField.text);
        }
        [UIView animateWithDuration:0.2 animations:^{
            self.transform = CGAffineTransformMakeScale(0.1, 0.1);
            self.alpha = 0;
        }];
    
    }
}


#pragma mark - getter
- (UIView *)backView {
    if (_backView) {
        return _backView;
    }
    _backView = [[UIView alloc]init];
    _backView.layer.masksToBounds = YES;
    _backView.layer.cornerRadius = 10;
    return _backView;
}

- (UILabel *)titleLabel {
    if (_titleLabel) {
        return _titleLabel;
    }
    _titleLabel = [[UILabel alloc]init];
    _titleLabel.text = @"标签";
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    _titleLabel.font = [UIFont systemFontOfSize:17];
    return _titleLabel;
}
- (UITextField *)textField {
    if (_textField) {
        return _textField;
    }
    _textField = [[UITextField alloc]init];;
    _textField.placeholder = @"请输入标签内容";
    _textField.layer.borderWidth = 1;
    _textField.layer.borderColor = [UIColor colorWithRed:0.7509 green:0.7509 blue:0.7509 alpha:1.0].CGColor;
    _textField.borderStyle = UITextBorderStyleRoundedRect;
    _textField.layer.masksToBounds = YES;
    _textField.layer.cornerRadius = 10;
    _textField.autocapitalizationType = NO;
    return _textField;
}


- (UIButton *)cancelButton {
    if (_cancelButton) {
        return _cancelButton;
    }
    _cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _cancelButton.layer.masksToBounds = YES;
    _cancelButton.layer.cornerRadius = 10;
    [_cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [_cancelButton addTarget:self action:@selector(cancelButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    _cancelButton.backgroundColor = [UIColor redColor];
    return _cancelButton;
}

- (UIButton *)sureButton {
    if (_sureButton) {
        return _sureButton;
    }
    _sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_sureButton setTitle:@"确定" forState:UIControlStateNormal];
    _sureButton.layer.masksToBounds = YES;
    _sureButton.layer.cornerRadius = 10;
    [_sureButton addTarget:self action:@selector(sureButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    _sureButton.backgroundColor = [UIColor redColor];
    return _sureButton;
}

@end
